home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 16605 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.8 KB

  1. Path: news2.near.net!ceylon!news
  2. From: Tony Confrey <ac11@gte.com>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: sprintf() in C++
  5. Date: Thu, 11 Apr 1996 10:03:41 -0400
  6. Organization: GTE Labs
  7. Message-ID: <316D113D.167E@gte.com>
  8. References: <4kene4$nkb@post.gsfc.nasa.gov>
  9. NNTP-Posting-Host: sambuca.gte.com
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.01 (X11; I; AIX 2)
  14. CC: vonrosen@nssdca.gsfc.nasa.gov
  15.  
  16. tycho wrote:
  17. > I could use some help with regard to sprintf() in C++.  It seems that
  18. > cout and cin were invented to avoid printf() and scanf() because the
  19. > latter have variable argument lists and hence can't be checked against
  20. > function prototypes.  But is there any corresponding C++ approach to
  21. > sprintf() and sscanf()?  When I look at various string class functions
  22. > I don't see what to use instead of sprintf() and sscanf().  But when I
  23. > look at my C code, I'm using sprintf() and sscanf() all the time.
  24. ...
  25. > Tycho von Rosenvinge
  26.  
  27. Check out "C++ IOStreams Handbook" by Steve Teale, Addison Wesley. 
  28. It goes into more detail than you'd ever want to know wrt iostreams.
  29.  
  30. The short answer to your question is that cin and cout are instances
  31. of the istream and ostream classes which deal with standard input
  32. and output. There are also ifstream and ofstream classes that deal with
  33. input and output on files, eg:
  34.  
  35. ofstream  fout ("myfile.txt");
  36. fout << "Output to a file." << endl;
  37.  
  38. But what you're looking for is istrstream and ostrstream classes - 
  39. input and output to memory buffers, eg:
  40.  
  41. char myarray[128];
  42. ostrstream os(myarray, 128);
  43. os << "Output to an in memory array!" << ends; // Note ends not endl
  44.  
  45. If you just want to do the standard stuff (not write your own 
  46. stream class) then the first 3 chapters of Teale's book give you all
  47. the information you need.
  48.  
  49.  
  50. Tony
  51.